//
//  YTMyViewController.m
//  YuTang
//
//  Created by 葛悦彣 on 2024/7/1.
//

#import "YTMyViewController.h"
#import "YTMyHeaderView.h"
#import "YTMyFooterView.h"
#import "YTTipsShowView.h"
#import "YTMyModel.h"
#import "YTMyGLYTableViewCell.h"

#import "TemplateManageViewController.h"
#import "PersonnelListViewController.h"
#import "YTForgotPasswordViewController.h"
#import "YTChangeViewController.h"
#import "YTStashAllListViewController.h"
#import "YTStashRecordsOutListViewController.h"

@interface YTMyViewController ()
@property (nonatomic ,strong) YTMyModel * model;
@end

@implementation YTMyViewController
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self loadNewData];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.NoDataHidden = YES;
    [self setupView];


    // Do any additional setup after loading the view.
}
- (void)setupView{
    self.view.backgroundColor = KcolorHex(@"#EEF0F1");
    UIImageView * img = kimageView(@"my_top_bg");
    [self.view addSubview:img];
    [img mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.offset(0);
        make.height.offset(kwt(250));
    }];
    
    UILabel * applevar = [DTKTool labelWithText:[NSString stringWithFormat:@"当前版本 v%@",[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]] andFont:kFont(14) andTextColor:KcolorHex(@"#686868")];
    
    [self.view addSubview:applevar];
    [applevar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.offset(0);
        make.bottom.offset(kwt(-20));
    }];
    [self.view addSubview:self.tableview];
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.offset(0);
    }];
    
    
}
- (void)loadNewData{
    [self.view showBusyHUD];
    [SCNetManger PostAppAccountGetMyModelWithUserID:[UserInfoModel shared].userID andCompetionHandlerBlock:^(HTTPDicBase * model) {
        [self.view hideBusyHUD];
        if(model.code == 200){
            YTMyModel * myModel = [YTMyModel yy_modelWithDictionary:model.result];
            self.model = myModel;
            [self.tableview reloadData];
        } else {
            [self.view showWarning:model.message];
        }
    }];
}


-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    YTMyHeaderView * headerview = [YTMyHeaderView NewView];
    headerview.model = self.model;
    return headerview;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return kwt(150);
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
MJWeakSelf
    YTMyFooterView * footerview = [YTMyFooterView NewView];
    footerview.openSZBlock = ^{
        YTChangeViewController * vc = [YTChangeViewController new];
        vc.hidesBottomBarWhenPushed = YES;
        [weakSelf.navigationController pushViewController:vc animated:YES];
    };
    footerview.exitBlock = ^{
        [YTTipsShowView showviewWithTips:@"确认退出?" andRightBlock:^{
            [UserInfoModel deleteAccount];
        }];
    };
    footerview.openCKBlock = ^{
        YTStashRecordsOutListViewController * vc = [YTStashRecordsOutListViewController new];
        vc.hidesBottomBarWhenPushed = YES;
        [weakSelf.navigationController pushViewController:vc animated:YES];
    };
    return footerview;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return kwt(200);
}
- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if([UserInfoModel shared].userIdentity == 3){///员工
        return 0;
    }
    return  1;
  
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return kwt(170);

}

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    MJWeakSelf
    
    YTMyGLYTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"YTMyGLYTableViewCell"];
    if (cell == nil) {
        cell = [[YTMyGLYTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"YTMyGLYTableViewCell"];
    }
    cell.model = self.model;
    cell.openCKBlock = ^{
        YTStashAllListViewController * vc = [YTStashAllListViewController new];
        vc.hidesBottomBarWhenPushed = YES;
        [weakSelf.navigationController pushViewController:vc animated:YES];
    };
    cell.openMBBlock = ^{
        TemplateManageViewController * vc = [TemplateManageViewController new];
        vc.hidesBottomBarWhenPushed = YES;
        [weakSelf.navigationController pushViewController:vc animated:YES];
    };
    cell.openRYBlock = ^{
        
        PersonnelListViewController * vc = [PersonnelListViewController new];
        vc.hidesBottomBarWhenPushed = YES;
        [weakSelf.navigationController pushViewController:vc animated:YES];
    };
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell ;
    
    
}


//
//-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

//}

@end
